<template>
  <!-- 组织结构管理 -->
  <div>
    <div class="boderSet">
      <div class="top">
        <el-row>
          <el-col :span="24">
            <div class="grid-content bg-purple-dark">
              <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                <el-breadcrumb-item>组织结构管理</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </el-col>
        </el-row>

      </div>
      <!-- 面板 -->
      <div class="content">
        <el-row>
          <el-col :span="24">
            <div class="conten-top">
              <span>组织结构图</span>
            </div>
          </el-col>
        </el-row>
        <!-- ------------------------------------------------------------------- -->
        <el-row>
          <el-col :span="20">
            <div class="btn-group">
              <el-button type="primary" icon="el-icon-plus" style="padding: 12px;"
                @click="dialogVisible = true">添加部门</el-button>
              <el-button type="danger" icon="el-icon-plus" style="padding: 12px;"
                @click="delDepartment()">删除部门</el-button>
            </div>
          </el-col>
          <el-col :span="4">
            <div class="btn-group">
              <el-dropdown>
                 <el-button type="primary" style="background-color: white; color: #3F3F65; border-color: #3F3F65;"
                  @click="exportExcel">导出Excel<i class="el-icon-arrow-down el-icon--right"></i>
                                          </el-button>
                                        <el-dropdown-menu slot="dropdown">

                                              <el-dropdown-item>导出全部</el-dropdown-item>
                                              <el-dropdown-item>导出选中</el-dropdown-item>
                                          </el-dropdown-menu>
                                    </el-dropdown>
            </div>
          </el-col>
        </el-row>
        <div class="struct-content">
          <el-tree style=" border-radius: 10px; padding: 28px; margin-left: 15px;" :data="getDepartmentData"
            :props="defaultProps" @node-click="handleNodeClick" :default-expand-all=true :show-checkbox="true"
            :check-strictly="true" node-key="id" @check="checkedBox" ref="tree" id="treeData"></el-tree>
        </div>
      </div>
    </div>
    <!-- 模态框 -->
    <el-dialog title="添加部门" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <!-- 内容 -->
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="部门名称" prop="departName">
          <el-input v-model="ruleForm.departName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="departNum">
          <el-input v-model.number="ruleForm.departNum" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <!-- 按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
import toExcel from "./../../utils/toExcel";
export default {
  name: 'structureManage',
  data() {
    var departNameVal = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('部门名不能为空'));
      }
      callback();
    };
    var departNumVal = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('部门编号不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      }, 500)
    }
    return {
      dialogVisible: false,//控制模态框显示
      data: [],
      defaultProps: {
        children: 'departmentVos',
        label: 'departmentName'
      },
      fid: 0,
      ruleForm: {
        departName: '',
        departNum: ''
      },
      rules: {
        departName: { required: true, validator: departNameVal, trigger: 'blur' },
        departNum: { required: true, validator: departNumVal, trigger: 'blur' }
      }
    }


  },
  computed: {
    ...mapGetters(["getDepartmentData"])

  },
  methods: {
    ...mapActions(["showDepartment", "departmentAdd", 'delDepartmentById']),
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },
    handleNodeClick(data) {
      console.log("in", data);
    },
    checkedBox() { // 获取选择的数据
      let checkData = this.$refs.tree.getCheckedNodes().concat(this.$refs.tree.getHalfCheckedNodes());
      for (var i in checkData) {
        // 判断子节点是否存在子节点 如果存在直接请求并且提示 false
        if (!checkData[i].hasOwnProperty("children")) {
          // 判断是否只选择一个 如果存在直接请求并且提示 false
          if (checkData.length == 1) {
            console.log(checkData[0].id)
          } else {
            this.$message.error("只选择一个节点");
            this.$refs.tree.setCheckedKeys([]);
            return
          }
        } else {
          this.$message.error("只能选择当前分类最后的子分类");
          this.$refs.tree.setCheckedKeys([]);
          return
        }
      }
      console.log(checkData);
      let id = 0
      for (let i = 0; i < checkData.length; i++) {
        id = checkData[i].id
      }
      this.fid = id
      console.log('选择的数组', this.fid);
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    submitForm(formName) {//添加部门
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          this.departmentAdd({ departmentName: this.ruleForm.departName, departmentNumber: this.ruleForm.departNum, fid: this.fid }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.fid = 0;
              this.showDepartment();
              this.getDepartmentData;
              this.$refs[formName].resetFields();
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    delDepartment() {
      if (this.fid == 0) {
        this.$message({
          message: '请勾选需要删除的部门',
          type: 'warning',
          center: true
        });
        return
      };
      this.delDepartmentById(this.fid).then(res => {
        console.log("删除", res);
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.showDepartment();
          this.fid = 0
        }
      })
    },
    exportExcel() {
      if (this.getDepartmentData.length < 1) {
        this.$message({
          message: "请选择需要导出的内容",
          type: "error",
          center: true
        })
        return false
      }
      toExcel.getExcel("#treeData", "部门表数据")
    }
  },
  created() {
    this.showDepartment();
  },
  mounted() {
  },
}
</script>
<style  scoped>
.top {
  /* height: 32px; */
  /* border: 1px solid black; */
  padding: 10px 20px 25px 29px;
}

.conten-top {
  padding: 0 32px 15px 40px;
}

.boderSet {
  margin: 0 29px;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.btn-group {
  padding-left: 40px;
}

::v-deep .el-tree-node__content {
  display: flex;
  align-items: center;
  height: 38px;
  cursor: pointer;
}

::v-deep .el-tree-node__content {
  display: flex;
  align-items: center;
  height: 40px;
  cursor: pointer;
}

.struct-content {
  margin-top: 10px;
  width: 100%;
  min-height: 400px;
  border-radius: 10px;
  border-top: 1px solid rgb(212, 209, 209);
  background-color: rgb(255, 255, 255);
}
</style>
